# coding=UTF-8
# /*************************************************
# Copyright:
# Author:Maple_Leaves
# Date:
# Description: 这个文件生成基础无限文明
# **************************************************/
import sqlite3
import pandas as pd
import time
import matplotlib.pyplot as plt
import numpy as np


# 这个文件是基础sql支持
from my_sql_support import *


def read_sqlite():

    with sqlite3.connect("D:\Civilization_6_Mods\SteamMOD\数据库备份\\DebugGameplay_xp2.sqlite") as con:
        c = con.cursor()
        # 提取整个表packs，存到dataframe中
        # districts = pd.read_sql("SELECT * FROM Districts", con=con)
        # 按照条件提取表sounds中满足条件的元素，存到dataframe中
        # my_civ_traits = pd.read_sql("SELECT * FROM CivilizationTraits where CivilizationType=\"CIVILIZATION_CHINA\" ",
        #                         con=con)


        my_civ_traits = pd.read_sql("SELECT * FROM CivilizationTraits where CivilizationType in "
                                "(SELECT CivilizationType FROM Civilizations where StartingCivilizationLevelType = \"CIVILIZATION_LEVEL_FULL_CIV\")",
                                con=con)

        # my_leader_traits = pd.read_sql("SELECT * FROM LeaderTraits where LeaderType in "
        #                             "(SELECT LeaderType FROM Leaders where InheritFrom = \"LEADER_DEFAULT\")",
        #                             con=con)

        my_leader_traits = pd.read_sql("SELECT * FROM LeaderTraits where LeaderType in "
                                       "(SELECT LeaderType FROM Leaders where InheritFrom = \"LEADER_DEFAULT\") "
                                       "and TraitType in "
                                       "(SELECT TraitType FROM Traits where InternalOnly = 0 )",
                                       con=con)

        # 输出提取出的表的信息
        # 表的大小
        # print(districts.shape)
        print(my_civ_traits.shape)
        # 表中的数据类型
        # print(districts.dtypes)
        print(my_civ_traits.dtypes)
        # 前几行数据
        # print(districts.head())
        print("========================")
        print(my_civ_traits.head())

        print("================================================")

        print(my_leader_traits.shape)
        print(my_leader_traits.dtypes)
        # print(districts.head())
        print("========================")
        print(my_leader_traits.head())

        return my_civ_traits, my_leader_traits


def read_sqlite_startbias():
    with sqlite3.connect("D:\Civilization_6_Mods\SteamMOD\数据库备份\\DebugGameplay_xp2.sqlite") as con:
        c = con.cursor()
        # 提取整个表packs，存到dataframe中
        # districts = pd.read_sql("SELECT * FROM Districts", con=con)
        # 按照条件提取表sounds中满足条件的元素，存到dataframe中
        # my_civ_traits = pd.read_sql("SELECT * FROM CivilizationTraits where CivilizationType=\"CIVILIZATION_CHINA\" ",
        #                         con=con)

        my_terrains = pd.read_sql("SELECT * FROM Terrains where TerrainType != \'TERRAIN_OCEAN\'",
                                  con=con)

        my_features = pd.read_sql("SELECT * FROM Features where NaturalWonder = 0",
                                  con=con)
        # Impassable = 0 and

        my_resources = pd.read_sql("SELECT * FROM Resources where ResourceClassType !=\'RESOURCECLASS_ARTIFACT\' "
                                   "and Frequency != 0",
                                   con=con)

        # 输出提取出的表的信息
        # 表的大小

        print("read_sqlite_startbias")
        print(my_terrains.shape)
        # 表中的数据类型

        print(my_terrains.dtypes)
        # 前几行数据
        # print(districts.head())
        print("========================")
        print(my_terrains.head())

        print("================================================")
        print(my_features.shape)
        print(my_features.dtypes)
        print(my_features.head())

        print("================================================")
        print(my_resources.shape)
        print(my_resources.dtypes)
        print(my_resources.head())

        print("========================")

        return my_terrains, my_features, my_resources

def read_configuration():
    file_ins = open("D:\Civilization_6_Mods\SteamMOD\数据库备份\\Configurations_xp2.txt")
    file_lines = file_ins.readlines()

    players = []
    playerItems = []

    for line_i in file_lines:
        line_list_pre = line_i.split(":")[1]
        line_list_pre_2 = line_list_pre.split("\n")[0]
        line_list = line_list_pre_2.split("\t")
        if len(line_list) == 8:
            players.append(line_list)
        if len(line_list) == 6:
            playerItems.append(line_list)

    pass

    print(players[0])
    print(playerItems[0])

    return players, playerItems


def item_has_trait(item_type, my_civ_traits, my_leader_traits):
    for index, row in my_civ_traits.iterrows():
        if ("TRAIT_CIVILIZATION_"+item_type) in row["TraitType"]:
            print(row["TraitType"])
            return True, row["TraitType"]
    for index, row in my_leader_traits.iterrows():
        if (("TRAIT_LEADER_"+item_type) in row["TraitType"]) or (("TRAIT_LEADER_PACHACUTI_"+item_type) in row["TraitType"]):
            print(row["TraitType"])
            return True, row["TraitType"]
    return False, ""


def table_include_value(table, value):
    for xi in table:
        if xi == value:
            return True
    return False


def get_unique_units(my_civ_traits, my_leader_traits, players, playerItems):
    """找到合法的UU，这里就不区分UU和LU了，意义不大"""
    unique_units = []
    unique_units_name = []
    unique_units_desc = []
    for item in playerItems:
        if "Type=UNIT" in item[2]:
            unit = (item[2]).split("=")[1]
            has_trait, trait_kind = item_has_trait(unit, my_civ_traits, my_leader_traits)
            if has_trait and (not table_include_value(unique_units, unit)):
                unique_units.append(unit)
                unit_name = (item[3]).split("=")[1]
                unit_desc = (item[4]).split("=")[1]
                unique_units_name.append(unit_name)
                unique_units_desc.append(unit_desc)
            pass
        pass
    pass
    return unique_units, unique_units_name, unique_units_desc


def get_unique_improvements(my_civ_traits, my_leader_traits, players, playerItems):
    """找到合法的UI"""
    unique_improvements = []
    unique_improvements_name = []
    unique_improvements_desc = []
    for item in playerItems:
        if "Type=IMPROVEMENT" in item[2]:
            improvement = (item[2]).split("=")[1]
            has_trait, trait_kind = item_has_trait(improvement, my_civ_traits, my_leader_traits)
            if has_trait and (not table_include_value(unique_improvements, improvement)):
                unique_improvements.append(improvement)
                improvement_name = (item[3]).split("=")[1]
                improvement_desc = (item[4]).split("=")[1]
                unique_improvements_name.append(improvement_name)
                unique_improvements_desc.append(improvement_desc)
            pass
        pass
    pass
    return unique_improvements, unique_improvements_name, unique_improvements_desc


def get_unique_districts(my_civ_traits, my_leader_traits, players, playerItems):
    """找到合法的UD"""
    unique_districts = []
    unique_districts_name = []
    unique_districts_desc = []
    for item in playerItems:
        if "Type=DISTRICT" in item[2]:
            district = (item[2]).split("=")[1]
            has_trait, trait_kind = item_has_trait(district, my_civ_traits, my_leader_traits)
            if has_trait and (not table_include_value(unique_districts, district)):
                unique_districts.append(district)
                district_name = (item[3]).split("=")[1]
                district_desc = (item[4]).split("=")[1]
                unique_districts_name.append(district_name)
                unique_districts_desc.append(district_desc)
            pass
        pass
    pass
    return unique_districts, unique_districts_name, unique_districts_desc


def get_unique_buildings(my_civ_traits, my_leader_traits, players, playerItems):
    """找到合法的UB"""
    unique_buildings = []
    unique_buildings_name = []
    unique_buildings_desc = []
    for item in playerItems:
        if "Type=BUILDING" in item[2]:
            building = (item[2]).split("=")[1]
            has_trait, trait_kind = item_has_trait(building, my_civ_traits, my_leader_traits)
            if has_trait and (not table_include_value(unique_buildings, building)):
                unique_buildings.append(building)
                district_name = (item[3]).split("=")[1]
                district_desc = (item[4]).split("=")[1]
                unique_buildings_name.append(district_name)
                unique_buildings_desc.append(district_desc)
            pass
        pass
    pass
    return unique_buildings, unique_buildings_name, unique_buildings_desc


def not_have_string(mustNotHaveStringList, my_string):
    for xi in mustNotHaveStringList:
        if xi in my_string:
            return False
    return True


def get_civ_leader_trait(civ, leader):
    civ_trait = ""
    leader_trait = ""
    mustNotHaveStringList = [
        "UNIT", "IMPROVEMENT", "BUILDING", "DISTRICT",
        "STREET_CARNIVAL", "DOCKYARD",
        "LLANERO", "COMANDANTE_GENERAL", "HULCHE"

    ]
    for index, row in my_civ_traits.iterrows():
        if civ == row["CivilizationType"] and not_have_string(mustNotHaveStringList, row["TraitType"]):
            print(row["TraitType"])
            civ_trait = row["TraitType"]
    for index, row in my_leader_traits.iterrows():
        if leader == row["LeaderType"] and not_have_string(mustNotHaveStringList, row["TraitType"]):
            print(row["TraitType"])
            leader_trait = row["TraitType"]
    return civ_trait, leader_trait


def get_unique_abilities(my_civ_traits, my_leader_traits, players, playerItems):
    """找到合法的UA LA"""
    unique_uas = []
    unique_uas_name = []
    unique_uas_desc = []

    unique_las = []
    unique_las_name = []
    unique_las_desc = []
    for item in players:
        civ = (item[0]).split("=")[1]
        leader = (item[1]).split("=")[1]
        # 然后从civ和leader倒推TraitType
        civ_trait, leader_trait = get_civ_leader_trait(civ, leader)
        if civ_trait == "" or leader_trait == "":
            print("error=======================")
            print("civ=", civ)
            print("leader=", leader)
        else:
            if not table_include_value(unique_uas, civ_trait):
                unique_uas.append(civ_trait)
                name = (item[5]).split("=")[1]
                desc = (item[6]).split("=")[1]
                unique_uas_name.append(name)
                unique_uas_desc.append(desc)

            if not table_include_value(unique_las, leader_trait):
                unique_las.append(leader_trait)
                name = (item[2]).split("=")[1]
                desc = (item[3]).split("=")[1]
                unique_las_name.append(name)
                unique_las_desc.append(desc)

        pass
    pass
    return unique_uas, unique_uas_name, unique_uas_desc, unique_las, unique_las_name, unique_las_desc


def create_config_parameter_file_uu(my_civ_traits, my_leader_traits, players, playerItems):

    sql_filename = "FCAC_Parameters_UU"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_UU", "Game",
                    "CONFIG_INFINITE_FREE_UU", 0,
                    "LOC_PAR_INFINITE_FREE_UU_DESCRIPTION", "DOMAIN_INFINITE_FREE_UU", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_UU_NAME", 4001]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)


    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_dv_info_0 = ["DOMAIN_INFINITE_FREE_UU", 0,
                    "LOC_DOMAIN_INFINITE_FREE_UU_0_NAME",
                    "LOC_DOMAIN_INFINITE_FREE_UU_0_DESCRIPTION",
                    0]
    dv_item_info.append(uu_dv_info_0)

    uu_total_list = []
    unique_units, unique_units_name, unique_units_desc = get_unique_units(my_civ_traits, my_leader_traits,
                                                                          players, playerItems)

    # 排个序，按照字母顺序
    unique_units = sorted(unique_units)
    unique_units_name = sorted(unique_units_name)
    unique_units_desc = sorted(unique_units_desc)

    for i in range(len(unique_units)):
        uu_total_list.append(unique_units[i])
        uu_dv_info = ["DOMAIN_INFINITE_FREE_UU", i+1,
                      unique_units_name[i],
                      unique_units_desc[i],
                      i+1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(unique_units)

    # 一些很奇怪的无法识别的单位
    extra_units = ["UNIT_COLOMBIAN_LLANERO", "UNIT_COMANDANTE_GENERAL", "UNIT_DE_ZEVEN_PROVINCIEN",
                   "UNIT_HUNGARY_BLACK_ARMY", "UNIT_MACEDONIAN_HETAIROI", "UNIT_MACEDONIAN_HYPASPIST",
                   "UNIT_MAYAN_HULCHE", "UNIT_PERSIAN_IMMORTAL"]
    extra_units_name = ["LOC_" + xi + "_NAME" for xi in extra_units]
    extra_units_desc = ["LOC_" + xi + "_DESCRIPTION" for xi in extra_units]
    for i in range(len(extra_units)):
        uu_total_list.append(extra_units[i])
        uu_dv_info = ["DOMAIN_INFINITE_FREE_UU", i + uu_num + 1,
                      extra_units_name[i],
                      extra_units_desc[i],
                      i + uu_num + 1]
        dv_item_info.append(uu_dv_info)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # 创建对应的选择性加载sql
    uu_generate_file_folder = ("C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
                               +"\Data\\FreeChooseAbilities\\UniqueUnits")
    uu_sql_filename_0 = "FCAC_UU_0"
    uu_sql_path_0 = generate_sql_file(uu_generate_file_folder, uu_sql_filename_0, author)

    civ_traits_name = "CivilizationTraits"
    civ_traits_key_list = ["CivilizationType", "TraitType"]
    fcac_trai_kinds_uu = []

    uu_sql_file_list = []
    uu_sql_file_list.append(uu_sql_path_0)
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]
        has_trait, trait_kind = item_has_trait(unit, my_civ_traits, my_leader_traits)

        if unit == "UNIT_COLOMBIAN_LLANERO":
            trait_kind = "TRAIT_CIVILIZATION_LLANERO"
        elif unit == "UNIT_COMANDANTE_GENERAL":
            trait_kind = "TRAIT_CIVILIZATION_COMANDANTE_GENERAL"
        elif unit == "UNIT_DE_ZEVEN_PROVINCIEN":
            trait_kind = "TRAIT_CIVILIZATION_UNIT_DUTCH_ZEVEN_PROVINCIEN"
        elif unit == "UNIT_HUNGARY_BLACK_ARMY":
            trait_kind = "TRAIT_LEADER_UNIT_MATTHIAS_BLACK_ARMY"
        elif unit == "UNIT_MACEDONIAN_HETAIROI":
            trait_kind = "TRAIT_LEADER_UNIT_HETAIROI"
        elif unit == "UNIT_MACEDONIAN_HYPASPIST":
            trait_kind = "TRAIT_CIVILIZATION_UNIT_HYPASPIST"
        elif unit == "UNIT_MAYAN_HULCHE":
            trait_kind = "TRAIT_CIVILIZATION_HULCHE"
        elif unit == "UNIT_PERSIAN_IMMORTAL":
            trait_kind = "TRAIT_CIVILIZATION_UNIT_IMMORTAL"

        fcac_trai_kinds_uu.append([trait_kind])
        cv_info = ["CIVILIZATION_INFINITE", trait_kind]

        uu_sql_filename_i = "FCAC_UU_"+str(i+1)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        insert_into_table_select_where_exists(uu_sql_path_i, civ_traits_name, civ_traits_key_list,  cv_info,
                                 extra_order="or ignore ", table1="Traits", key1="TraitType", value1=trait_kind)

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/UniqueUnits"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder, "CONFIG_INFINITE_FREE_UU", "FCAC_UU")

    # 加入随机化的文件
    # random_sql_path

    fcac_table_name = "FCAC_UnitTraits"
    fcac_key_list = ["TraitType"]
    fcac_uu_info = fcac_trai_kinds_uu

    insert_into_table_values(random_sql_path, fcac_table_name, fcac_key_list, fcac_uu_info, extra_order="or ignore ")
    add_empty_rows_to_sql(random_sql_path, 2)


def create_config_parameter_file_lu(my_civ_traits, my_leader_traits, players, playerItems):

    sql_filename = "FCAC_Parameters_LU"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_LU", "Game",
                    "CONFIG_INFINITE_FREE_LU", 0,
                    "LOC_PAR_INFINITE_FREE_LU_DESCRIPTION", "DOMAIN_INFINITE_FREE_LU", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_LU_NAME", 4002]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)


    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_dv_info_0 = ["DOMAIN_INFINITE_FREE_LU", 0,
                    "LOC_DOMAIN_INFINITE_FREE_LU_0_NAME",
                    "LOC_DOMAIN_INFINITE_FREE_LU_0_DESCRIPTION",
                    0]
    dv_item_info.append(uu_dv_info_0)

    uu_total_list = []
    unique_units, unique_units_name, unique_units_desc = get_unique_units(my_civ_traits, my_leader_traits,
                                                                          players, playerItems)

    # 排个序，按照字母顺序
    unique_units = sorted(unique_units)
    unique_units_name = sorted(unique_units_name)
    unique_units_desc = sorted(unique_units_desc)

    for i in range(len(unique_units)):
        uu_total_list.append(unique_units[i])
        uu_dv_info = ["DOMAIN_INFINITE_FREE_LU", i+1,
                      unique_units_name[i],
                      unique_units_desc[i],
                      i+1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(unique_units)

    # 一些很奇怪的无法识别的单位
    extra_units = ["UNIT_COLOMBIAN_LLANERO", "UNIT_COMANDANTE_GENERAL", "UNIT_DE_ZEVEN_PROVINCIEN",
                   "UNIT_HUNGARY_BLACK_ARMY", "UNIT_MACEDONIAN_HETAIROI", "UNIT_MACEDONIAN_HYPASPIST",
                   "UNIT_MAYAN_HULCHE", "UNIT_PERSIAN_IMMORTAL"]
    extra_units_name = ["LOC_" + xi + "_NAME" for xi in extra_units]
    extra_units_desc = ["LOC_" + xi + "_DESCRIPTION" for xi in extra_units]
    for i in range(len(extra_units)):
        uu_total_list.append(extra_units[i])
        uu_dv_info = ["DOMAIN_INFINITE_FREE_LU", i + uu_num + 1,
                      extra_units_name[i],
                      extra_units_desc[i],
                      i + uu_num + 1]
        dv_item_info.append(uu_dv_info)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # 创建对应的选择性加载sql
    uu_generate_file_folder = ("C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
                               +"\Data\\FreeChooseAbilities\\LeaderUniqueUnits")
    uu_sql_filename_0 = "FCAC_LU_0"
    uu_sql_path_0 = generate_sql_file(uu_generate_file_folder, uu_sql_filename_0, author)

    civ_traits_name = "LeaderTraits"
    civ_traits_key_list = ["LeaderType", "TraitType"]

    uu_sql_file_list = []
    uu_sql_file_list.append(uu_sql_path_0)
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]
        has_trait, trait_kind = item_has_trait(unit, my_civ_traits, my_leader_traits)

        if unit == "UNIT_COLOMBIAN_LLANERO":
            trait_kind = "TRAIT_CIVILIZATION_LLANERO"
        elif unit == "UNIT_COMANDANTE_GENERAL":
            trait_kind = "TRAIT_CIVILIZATION_COMANDANTE_GENERAL"
        elif unit == "UNIT_DE_ZEVEN_PROVINCIEN":
            trait_kind = "TRAIT_CIVILIZATION_UNIT_DUTCH_ZEVEN_PROVINCIEN"
        elif unit == "UNIT_HUNGARY_BLACK_ARMY":
            trait_kind = "TRAIT_LEADER_UNIT_MATTHIAS_BLACK_ARMY"
        elif unit == "UNIT_MACEDONIAN_HETAIROI":
            trait_kind = "TRAIT_LEADER_UNIT_HETAIROI"
        elif unit == "UNIT_MACEDONIAN_HYPASPIST":
            trait_kind = "TRAIT_CIVILIZATION_UNIT_HYPASPIST"
        elif unit == "UNIT_MAYAN_HULCHE":
            trait_kind = "TRAIT_CIVILIZATION_HULCHE"
        elif unit == "UNIT_PERSIAN_IMMORTAL":
            trait_kind = "TRAIT_CIVILIZATION_UNIT_IMMORTAL"

        cv_info = ["LEADER_INFINITE", trait_kind]

        uu_sql_filename_i = "FCAC_LU_"+str(i+1)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        insert_into_table_select_where_exists(uu_sql_path_i, civ_traits_name, civ_traits_key_list,  cv_info,
                                 extra_order="or ignore ", table1="Traits", key1="TraitType", value1=trait_kind)

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/LeaderUniqueUnits"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_LU", "FCAC_LU")


def create_config_parameter_file_ui(my_civ_traits, my_leader_traits, players, playerItems):

    sql_filename = "FCAC_Parameters_UI"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_UI", "Game",
                    "CONFIG_INFINITE_FREE_UI", 0,
                    "LOC_PAR_INFINITE_FREE_UI_DESCRIPTION", "DOMAIN_INFINITE_FREE_UI", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_UI_NAME", 4003]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)


    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_dv_info_0 = ["DOMAIN_INFINITE_FREE_UI", 0,
                    "LOC_DOMAIN_INFINITE_FREE_UI_0_NAME",
                    "LOC_DOMAIN_INFINITE_FREE_UI_0_DESCRIPTION",
                    0]
    dv_item_info.append(uu_dv_info_0)

    uu_total_list = []
    unique_improvs, unique_improvs_name, unique_improvs_desc = get_unique_improvements(my_civ_traits,  my_leader_traits,
                                                                                       players, playerItems)

    # 排个序，按照字母顺序
    unique_improvs = sorted(unique_improvs)
    unique_improvs_name = sorted(unique_improvs_name)
    unique_improvs_desc = sorted(unique_improvs_desc)

    for i in range(len(unique_improvs)):
        uu_total_list.append(unique_improvs[i])
        uu_dv_info = ["DOMAIN_INFINITE_FREE_UI", i+1,
                      unique_improvs_name[i],
                      unique_improvs_desc[i],
                      i+1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(unique_improvs)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # return

    # 创建对应的选择性加载sql
    uu_generate_file_folder = ("C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
                               +"\Data\\FreeChooseAbilities\\UniqueImprovements")
    uu_sql_filename_0 = "FCAC_UI_0"
    uu_sql_path_0 = generate_sql_file(uu_generate_file_folder, uu_sql_filename_0, author)

    civ_traits_name = "CivilizationTraits"
    civ_traits_key_list = ["CivilizationType", "TraitType"]

    uu_sql_file_list = []
    uu_sql_file_list.append(uu_sql_path_0)
    fcac_trai_kinds_ui = []
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]
        has_trait, trait_kind = item_has_trait(unit, my_civ_traits, my_leader_traits)
        cv_info = ["CIVILIZATION_INFINITE", trait_kind]
        fcac_trai_kinds_ui.append([trait_kind])

        uu_sql_filename_i = "FCAC_UI_"+str(i+1)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        insert_into_table_select_where_exists(uu_sql_path_i, civ_traits_name, civ_traits_key_list,  cv_info,
                                 extra_order="or ignore ", table1="Traits", key1="TraitType", value1=trait_kind)

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/UniqueImprovements"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_UI", "FCAC_UI")

    # 加入随机化的文件
    # random_sql_path

    fcac_table_name = "FCAC_UITraits"
    fcac_key_list = ["TraitType"]
    fcac_uu_info = fcac_trai_kinds_ui

    insert_into_table_values(random_sql_path, fcac_table_name, fcac_key_list, fcac_uu_info, extra_order="or ignore ")
    add_empty_rows_to_sql(random_sql_path, 2)


def create_config_parameter_file_ud(my_civ_traits, my_leader_traits, players, playerItems):

    sql_filename = "FCAC_Parameters_UD"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_UD", "Game",
                    "CONFIG_INFINITE_FREE_UD", 0,
                    "LOC_PAR_INFINITE_FREE_UD_DESCRIPTION", "DOMAIN_INFINITE_FREE_UD", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_UD_NAME", 4004]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)


    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_dv_info_0 = ["DOMAIN_INFINITE_FREE_UD", 0,
                    "LOC_DOMAIN_INFINITE_FREE_UD_0_NAME",
                    "LOC_DOMAIN_INFINITE_FREE_UD_0_DESCRIPTION",
                    0]
    dv_item_info.append(uu_dv_info_0)

    uu_total_list = []
    unique_districts, unique_districts_name, unique_districts_desc = get_unique_districts(my_civ_traits,
                                                                                          my_leader_traits,
                                                                                          players, playerItems)

    # 排个序，按照字母顺序
    unique_districts = sorted(unique_districts)
    unique_districts_name = sorted(unique_districts_name)
    unique_districts_desc = sorted(unique_districts_desc)

    for i in range(len(unique_districts)):
        uu_total_list.append(unique_districts[i])
        uu_dv_info = ["DOMAIN_INFINITE_FREE_UD", i+1,
                      unique_districts_name[i],
                      unique_districts_desc[i],
                      i+1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(unique_districts)

    # 一些很奇怪的无法识别的UD
    extra_districts = ["DISTRICT_MBANZA", "DISTRICT_ROYAL_NAVY_DOCKYARD",
                   "DISTRICT_STREET_CARNIVAL", "DISTRICT_WATER_STREET_CARNIVAL"]
    extra_districts_name = ["LOC_" + xi + "_NAME" for xi in extra_districts]
    extra_districts_desc = ["LOC_" + xi + "_DESCRIPTION" for xi in extra_districts]
    for i in range(len(extra_districts)):
        uu_total_list.append(extra_districts[i])
        uu_dv_info = ["DOMAIN_INFINITE_FREE_UD", i + uu_num + 1,
                      extra_districts_name[i],
                      extra_districts_desc[i],
                      i + uu_num + 1]
        dv_item_info.append(uu_dv_info)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # return

    # 创建对应的选择性加载sql
    uu_generate_file_folder = ("C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
                               +"\Data\\FreeChooseAbilities\\UniqueDistricts")
    uu_sql_filename_0 = "FCAC_UD_0"
    uu_sql_path_0 = generate_sql_file(uu_generate_file_folder, uu_sql_filename_0, author)

    civ_traits_name = "CivilizationTraits"
    civ_traits_key_list = ["CivilizationType", "TraitType"]

    uu_sql_file_list = []
    uu_sql_file_list.append(uu_sql_path_0)
    fcac_trai_kinds_ud = []
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]

        if table_include_value(extra_districts, unit):
            if unit == "DISTRICT_MBANZA":
                trait_kind = "TRAIT_CIVILIZATION_MBANZA"
            elif unit == "DISTRICT_ROYAL_NAVY_DOCKYARD":
                trait_kind = "TRAIT_CIVILIZATION_ROYAL_NAVY_DOCKYARD"
            elif unit == "DISTRICT_STREET_CARNIVAL":
                trait_kind = "TRAIT_CIVILIZATION_STREET_CARNIVAL"
            elif unit == "DISTRICT_WATER_STREET_CARNIVAL":
                trait_kind = "TRAIT_CIVILIZATION_STREET_CARNIVAL"
            else:
                trait_kind = "TRAIT_NOT_EXISTS"
        else:
            has_trait, trait_kind = item_has_trait(unit, my_civ_traits, my_leader_traits)

        cv_info = ["CIVILIZATION_INFINITE", trait_kind]
        fcac_trai_kinds_ud.append([trait_kind])

        uu_sql_filename_i = "FCAC_UD_"+str(i+1)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        insert_into_table_select_where_exists(uu_sql_path_i, civ_traits_name, civ_traits_key_list,  cv_info,
                                 extra_order="or ignore ", table1="Traits", key1="TraitType", value1=trait_kind)

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/UniqueDistricts"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_UD", "FCAC_UD")

    # 加入随机化的文件
    # random_sql_path

    fcac_table_name = "FCAC_UDTraits"
    fcac_key_list = ["TraitType"]
    fcac_uu_info = fcac_trai_kinds_ud

    insert_into_table_values(random_sql_path, fcac_table_name, fcac_key_list, fcac_uu_info, extra_order="or ignore ")
    add_empty_rows_to_sql(random_sql_path, 2)


def create_config_parameter_file_ub(my_civ_traits, my_leader_traits, players, playerItems):

    sql_filename = "FCAC_Parameters_UB"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_UB", "Game",
                    "CONFIG_INFINITE_FREE_UB", 0,
                    "LOC_PAR_INFINITE_FREE_UB_DESCRIPTION", "DOMAIN_INFINITE_FREE_UB", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_UB_NAME", 4005]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)


    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_dv_info_0 = ["DOMAIN_INFINITE_FREE_UB", 0,
                    "LOC_DOMAIN_INFINITE_FREE_UB_0_NAME",
                    "LOC_DOMAIN_INFINITE_FREE_UB_0_DESCRIPTION",
                    0]
    dv_item_info.append(uu_dv_info_0)

    uu_total_list = []
    unique_buildings, unique_buildings_name, unique_buildings_desc = get_unique_buildings(my_civ_traits,
                                                                                          my_leader_traits,
                                                                                          players, playerItems)

    # 排个序，按照字母顺序
    unique_buildings = sorted(unique_buildings)
    unique_buildings_name = sorted(unique_buildings_name)
    unique_buildings_desc = sorted(unique_buildings_desc)

    for i in range(len(unique_buildings)):
        uu_total_list.append(unique_buildings[i])
        uu_dv_info = ["DOMAIN_INFINITE_FREE_UB", i+1,
                      unique_buildings_name[i],
                      unique_buildings_desc[i],
                      i+1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(unique_buildings)

    # 一些很奇怪的无法识别的UB
    # extra_districts = ["DISTRICT_MBANZA", "DISTRICT_ROYAL_NAVY_DOCKYARD",
    #                "DISTRICT_STREET_CARNIVAL", "DISTRICT_WATER_STREET_CARNIVAL"]
    # extra_districts_name = ["LOC_" + xi + "_NAME" for xi in extra_districts]
    # extra_districts_desc = ["LOC_" + xi + "_DESCRIPTION" for xi in extra_districts]
    # for i in range(len(extra_districts)):
    #     uu_total_list.append(extra_districts[i])
    #     uu_dv_info = ["DOMAIN_INFINITE_FREE_UB", i + uu_num + 1,
    #                   extra_districts_name[i],
    #                   extra_districts_desc[i],
    #                   i + uu_num + 1]
    #     dv_item_info.append(uu_dv_info)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # return

    # 创建对应的选择性加载sql
    uu_generate_file_folder = ("C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
                               +"\Data\\FreeChooseAbilities\\UniqueBuildings")
    uu_sql_filename_0 = "FCAC_UB_0"
    uu_sql_path_0 = generate_sql_file(uu_generate_file_folder, uu_sql_filename_0, author)

    civ_traits_name = "CivilizationTraits"
    civ_traits_key_list = ["CivilizationType", "TraitType"]

    uu_sql_file_list = []
    uu_sql_file_list.append(uu_sql_path_0)
    fcac_trai_kinds_ub = []
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]
        has_trait, trait_kind = item_has_trait(unit, my_civ_traits, my_leader_traits)
        cv_info = ["CIVILIZATION_INFINITE", trait_kind]
        fcac_trai_kinds_ub.append([trait_kind])

        uu_sql_filename_i = "FCAC_UB_"+str(i+1)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        insert_into_table_select_where_exists(uu_sql_path_i, civ_traits_name, civ_traits_key_list,  cv_info,
                                 extra_order="or ignore ", table1="Traits", key1="TraitType", value1=trait_kind)

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/UniqueBuildings"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_UB", "FCAC_UB")

    # 加入随机化的文件
    # random_sql_path

    fcac_table_name = "FCAC_UBTraits"
    fcac_key_list = ["TraitType"]
    fcac_uu_info = fcac_trai_kinds_ub

    insert_into_table_values(random_sql_path, fcac_table_name, fcac_key_list, fcac_uu_info, extra_order="or ignore ")
    add_empty_rows_to_sql(random_sql_path, 2)


def create_config_parameter_file_ua(my_civ_traits, my_leader_traits, players, playerItems):

    sql_filename = "FCAC_Parameters_UA"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_UA", "Game",
                    "CONFIG_INFINITE_FREE_UA", 0,
                    "LOC_PAR_INFINITE_FREE_UA_DESCRIPTION", "DOMAIN_INFINITE_FREE_UA", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_UA_NAME", 3999]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)


    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_dv_info_0 = ["DOMAIN_INFINITE_FREE_UA", 0,
                    "LOC_DOMAIN_INFINITE_FREE_UA_0_NAME",
                    "LOC_DOMAIN_INFINITE_FREE_UA_0_DESCRIPTION",
                    0]
    dv_item_info.append(uu_dv_info_0)

    uu_total_list = []
    uas, uas_name, uas_desc, las, las_name, las_desc = get_unique_abilities(my_civ_traits, my_leader_traits,
                                                                            players, playerItems)

    # 排个序，按照字母顺序
    # 现在不能排序，因为希腊文化融合
    # uas = sorted(uas)
    # uas_name = sorted(uas_name)
    # uas_desc = sorted(uas_desc)

    for i in range(len(uas)):
        uu_total_list.append(uas[i])
        uu_dv_info = ["DOMAIN_INFINITE_FREE_UA", i+1,
                      uas_name[i],
                      uas_desc[i],
                      i+1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(uas)

    # 一些很奇怪的无法识别的UB
    extra_districts = ["TRAIT_CIVILIZATION_ADJACENT_DISTRICTS"]
    extra_districts_name = ["LOC_" + xi + "_NAME" for xi in extra_districts]
    extra_districts_desc = ["LOC_" + xi + "_DESCRIPTION" for xi in extra_districts]
    for i in range(len(extra_districts)):
        uu_total_list.append(extra_districts[i])
        uu_dv_info = ["DOMAIN_INFINITE_FREE_UA", i + uu_num + 1,
                      extra_districts_name[i],
                      extra_districts_desc[i],
                      i + uu_num + 1]
        dv_item_info.append(uu_dv_info)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # return

    # 创建对应的选择性加载sql
    uu_generate_file_folder = ("C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
                               +"\Data\\FreeChooseAbilities\\UniqueAbilities")
    uu_sql_filename_0 = "FCAC_UA_0"
    uu_sql_path_0 = generate_sql_file(uu_generate_file_folder, uu_sql_filename_0, author)

    civ_traits_name = "CivilizationTraits"
    civ_traits_key_list = ["CivilizationType", "TraitType"]

    uu_sql_file_list = []
    uu_sql_file_list.append(uu_sql_path_0)
    fcac_trai_kinds_ua = []
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]
        trait_kind = unit
        cv_info = ["CIVILIZATION_INFINITE", trait_kind]
        fcac_trai_kinds_ua.append([trait_kind])

        uu_sql_filename_i = "FCAC_UA_"+str(i+1)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        insert_into_table_select_where_exists(uu_sql_path_i, civ_traits_name, civ_traits_key_list,  cv_info,
                                 extra_order="or ignore ", table1="Traits", key1="TraitType", value1=trait_kind)

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/UniqueAbilities"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_UA", "FCAC_UA")

    # 加入随机化的文件
    # random_sql_path

    fcac_table_name = "FCAC_UATraits"
    fcac_key_list = ["TraitType"]
    fcac_uu_info = fcac_trai_kinds_ua

    insert_into_table_values(random_sql_path, fcac_table_name, fcac_key_list, fcac_uu_info, extra_order="or ignore ")
    add_empty_rows_to_sql(random_sql_path, 2)


def create_config_parameter_file_la(my_civ_traits, my_leader_traits, players, playerItems):

    sql_filename = "FCAC_Parameters_LA"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_LA", "Game",
                    "CONFIG_INFINITE_FREE_LA", 0,
                    "LOC_PAR_INFINITE_FREE_LA_DESCRIPTION", "DOMAIN_INFINITE_FREE_LA", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_LA_NAME", 4000]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)


    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_dv_info_0 = ["DOMAIN_INFINITE_FREE_LA", 0,
                    "LOC_DOMAIN_INFINITE_FREE_LA_0_NAME",
                    "LOC_DOMAIN_INFINITE_FREE_LA_0_DESCRIPTION",
                    0]
    dv_item_info.append(uu_dv_info_0)

    uu_total_list = []
    uas, uas_name, uas_desc, las, las_name, las_desc = get_unique_abilities(my_civ_traits, my_leader_traits,
                                                                            players, playerItems)

    # 排个序，按照字母顺序
    # las_name = sorted(las_name)

    for i in range(len(las)):
        uu_total_list.append(las[i])
        uu_dv_info = ["DOMAIN_INFINITE_FREE_LA", i+1,
                      las_name[i],
                      las_desc[i],
                      i+1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(las)

    # 一些很奇怪的无法识别的UA
    extra_las = ["TRAIT_LEADER_DIVINE_WIND",
                 "TRAIT_LEADER_MELEE_COASTAL_RAIDS",
                 "TRAIT_LEADER_TOKUGAWA"
                ]
    extra_las_name = ["LOC_TRAIT_LEADER_DIVINE_WIND_NAME",
                      "LOC_TRAIT_LEADER_THUNDERBOLT_NAME",
                      "LOC_TRAIT_LEADER_TOKUGAWA_NAME"
                      ]
    extra_las_desc = ["LOC_TRAIT_LEADER_DIVINE_WIND_EXPANSION2_DESCRIPTION",
                      "LOC_TRAIT_LEADER_THUNDERBOLT_EXPANSION2_DESCRIPTION",
                      "LOC_TRAIT_LEADER_TOKUGAWA_XP_DESCRIPTION"
                      ]

    for i in range(len(extra_las)):
        uu_total_list.append(extra_las[i])
        uu_dv_info = ["DOMAIN_INFINITE_FREE_LA", i + uu_num + 1,
                      extra_las_name[i],
                      extra_las_desc[i],
                      i + uu_num + 1]
        dv_item_info.append(uu_dv_info)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # return

    # 创建对应的选择性加载sql
    uu_generate_file_folder = ("C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
                               +"\Data\\FreeChooseAbilities\\LeaderUniqueAbilities")
    uu_sql_filename_0 = "FCAC_LA_0"
    uu_sql_path_0 = generate_sql_file(uu_generate_file_folder, uu_sql_filename_0, author)

    civ_traits_name = "LeaderTraits"
    civ_traits_key_list = ["LeaderType", "TraitType"]

    uu_sql_file_list = []
    uu_sql_file_list.append(uu_sql_path_0)
    fcac_trai_kinds_la = []
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]
        trait_kind = unit
        cv_info = ["LEADER_INFINITE", trait_kind]
        fcac_trai_kinds_la.append([trait_kind])

        uu_sql_filename_i = "FCAC_LA_"+str(i+1)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        insert_into_table_select_where_exists(uu_sql_path_i, civ_traits_name, civ_traits_key_list,  cv_info,
                                 extra_order="or ignore ", table1="Traits", key1="TraitType", value1=trait_kind)

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/LeaderUniqueAbilities"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_LA", "FCAC_LA")

    # 加入随机化的文件
    # random_sql_path

    fcac_table_name = "FCAC_LATraits"
    fcac_key_list = ["TraitType"]
    fcac_uu_info = fcac_trai_kinds_la

    insert_into_table_values(random_sql_path, fcac_table_name, fcac_key_list, fcac_uu_info, extra_order="or ignore ")
    add_empty_rows_to_sql(random_sql_path, 2)


def add_localized_text():
    """在mod信息之后添加Mod本地化文本"""
    modinfo_path = ("C:\\Users\User\Documents\My Games\Sid Meier's Civilization VI\Mods\FreeChooseAbilityCivilization"
                    + "\\" + "FreeChooseAbilityCivilization.modinfo")
    file_ins = open(modinfo_path, "r+", encoding='utf-8')
    file_lines = file_ins.readlines()
    new_file_lines = []

    mod_id = "FCAC_INFINITE"
    mod_name_key = "LOC_MY_MOD_NAME"+"_"+mod_id
    mod_desc_key = "LOC_MY_MOD_DESCRIPTION" + "_" + mod_id
    mod_teaser_key = "LOC_MY_MOD_TEASER" + "_" + mod_id
    mod_authors_key = "LOC_MY_MOD_AUTHORS" + "_" + mod_id
    mod_thanks_key = "LOC_MY_MOD_SPECIALTHANKS" + "_" + mod_id

    mod_loc_key_list = [mod_name_key, mod_desc_key, mod_teaser_key, mod_authors_key, mod_thanks_key]

    mod_loc_value_list_zh_Hans_CN = ["无限文明：自选特性",
                                     "引入了一个无限文明，可以自选UA，LA，UU，以及出生关联等"
                                     "（仅限游戏原版特性、资源等，需要相应的DLC）。"
                                     "[NEWLINE][NEWLINE]推荐在原版环境使用，其他环境下表现未知。"
                                     "[NEWLINE][NEWLINE]最新更新：修改了城市命名方式。",
                                     "可以自选特性！",
                                     "枫叶(Maple_Leaves)",
                                     "Firaxis，python,，francis"]
    mod_loc_value_list_zh_Hant_HK = ["无限文明：自选特性",
                                     "引入了一个无限文明，可以自选UA，LA，UU，以及出生关联等"
                                     "（仅限游戏原版特性、资源等，需要相应的DLC）。"
                                     "[NEWLINE][NEWLINE]推荐在原版环境使用，其他环境下表现未知。"
                                     "[NEWLINE][NEWLINE]最新更新：修改了城市命名方式。",
                                     "可以自选特性！",
                                     "枫叶(Maple_Leaves)",
                                     "Firaxis，python，francis"]
    en_us_desc = ("An infinite civilization has been introduced, whose unique abilities can be selected by yourself, "
                  "like UA, LA, UU, StartBias, etc. (only supports the abilities in game, the corresponding DLC is required). "
                   "[NEWLINE][NEWLINE]It is recommended to use this mod not with mods (that heavily modify the database)."
                  "[NEWLINE][NEWLINE]Recent updates: changed the city names.")

    mod_loc_value_list_en_US = ["Infinite Civilization: Choose your own Abilities",
                                en_us_desc,
                                "Choose Abilities Freely!",
                                "Maple_Leaves",
                                "Firaxis, python, francis"]

    textAdded = False

    for line_i in file_lines:
        if "</Name>" in line_i:
            new_file_lines.append("\t\t<Name>{}</Name>\n".format(mod_name_key))
        elif "</Description>" in line_i:
            new_file_lines.append("\t\t<Description>{}</Description>\n".format(mod_desc_key))
        elif "</Teaser>" in line_i:
            new_file_lines.append("\t\t<Teaser>{}</Teaser>\n".format(mod_teaser_key))
        elif "</Authors>" in line_i:
            new_file_lines.append("\t\t<Authors>{}</Authors>\n".format(mod_authors_key))
        elif "</SpecialThanks>" in line_i:
            new_file_lines.append("\t\t<SpecialThanks>{}</SpecialThanks>\n".format(mod_thanks_key))
        elif ("</Properties>" in line_i) and (not textAdded):
            new_file_lines.append(line_i)

            # 添加文本
            new_file_lines.append("\t<LocalizedText>\n")

            for i in range(5):
                new_file_lines.append("\t\t<Text id=\"{}\">\n".format(mod_loc_key_list[i]))
                new_file_lines.append("\t\t\t<en_US>{}</en_US>\n".format(mod_loc_value_list_en_US[i]))
                new_file_lines.append("\t\t\t<zh_Hans_CN>{}</zh_Hans_CN>\n".format(mod_loc_value_list_zh_Hans_CN[i]))
                new_file_lines.append("\t\t\t<zh_Hant_HK>{}</zh_Hant_HK>\n".format(mod_loc_value_list_zh_Hant_HK[i]))
                new_file_lines.append("\t\t</Text>\n")

            new_file_lines.append("\t</LocalizedText>\n")
            textAdded = True

        else:
            new_file_lines.append(line_i)
        pass

    file_ins.seek(0)
    for xi in new_file_lines:
        file_ins.write(xi)

    # 关闭文件
    file_ins.close()
    pass


def delete_criterias(node_num):
    modinfo_path = ("C:\\Users\User\Documents\My Games\Sid Meier's Civilization VI\Mods\FreeChooseAbilityCivilization"
                    + "\\" + "FreeChooseAbilityCivilization.modinfo")
    file_ins = open(modinfo_path, "r+", encoding='utf-8')
    file_lines = file_ins.readlines()
    new_file_lines = []

    cri_begin = False
    cri_end_count = 0
    for line_i in file_lines:
        if "<ActionCriteria>" in line_i:
            if not cri_begin:
                cri_begin = True
                new_file_lines.append(line_i)

        elif "</ActionCriteria>" in line_i:
            cri_end_count = cri_end_count + 1
            if cri_end_count == node_num:
                new_file_lines.append(line_i)
        else:
            new_file_lines.append(line_i)
        pass

    file_ins.seek(0)
    for xi in new_file_lines:
        file_ins.write(xi)

    # 关闭文件
    file_ins.close()
    pass


def add_ActionCriteria(RuleSetInUse, Group, ConfigurationId, Value, ExtraStr):
    criteria_list = []
    Criteria_id = ExtraStr+"_CRI_" + ConfigurationId + "_" + str(Value)
    criteria_list.append("\t<Criteria id=\"{}\">\n".format(Criteria_id))
    criteria_list.append("\t\t<RuleSetInUse>{}</RuleSetInUse>\n".format(RuleSetInUse))
    criteria_list.append("\t\t<ConfigurationValueMatches>\n")
    criteria_list.append("\t\t\t<Group>{}</Group>\n".format(Group))
    criteria_list.append("\t\t\t<ConfigurationId>{}</ConfigurationId>\n".format(ConfigurationId))
    criteria_list.append("\t\t\t<Value>{}</Value>\n".format(Value))
    criteria_list.append("\t\t</ConfigurationValueMatches>\n")
    criteria_list.append("\t</Criteria>\n")

    return criteria_list


def add_UpdateDatabase(Criteria_id, LoadOrder, File, Folder):
    updatedatabase_list = []
    updatedatabase_list.append("\t<UpdateDatabase id=\"{}\">\n".format("database_" + Criteria_id))
    updatedatabase_list.append("\t\t<Criteria>{}</Criteria>\n".format(Criteria_id))
    updatedatabase_list.append("\t\t<Properties>\n")
    updatedatabase_list.append("\t\t\t<LoadOrder>{}</LoadOrder>\n".format(LoadOrder))
    updatedatabase_list.append("\t\t</Properties>\n")
    updatedatabase_list.append("\t\t<File>{}/{}.sql</File>\n".format(Folder, File))
    updatedatabase_list.append("\t</UpdateDatabase>\n")

    return updatedatabase_list


def add_to_modinfo_file(uu_total_list, uu_sql_file_list, Folder, ConfigurationId, ExtraStr, loadorder=300000):
    modinfo_path = ("C:\\Users\User\Documents\My Games\Sid Meier's Civilization VI\Mods\FreeChooseAbilityCivilization"
                    + "\\" + "FreeChooseAbilityCivilization.modinfo")
    file_ins = open(modinfo_path, "r+", encoding='utf-8')
    file_lines = file_ins.readlines()
    new_file_lines = []
    for line_i in file_lines:
        if "</Dependencies>" in line_i:
            new_file_lines.append(line_i)
            # 添加ActionCriteria
            new_file_lines.append("<ActionCriteria>\n")

            for i in range(len(uu_total_list)+1):
                criteria_list_i = add_ActionCriteria("RULESET_EXPANSION_2", "Game",
                                                   ConfigurationId, i, ExtraStr)
                for xi in criteria_list_i:
                    new_file_lines.append(xi)
            new_file_lines.append("</ActionCriteria>\n")

        elif "</InGameActions>" in line_i:

            # 添加InGameAction
            for i in range(len(uu_total_list) + 1):
                Criteria_id = ExtraStr + "_CRI_" + ConfigurationId + "_" + str(i)
                LoadOrder = loadorder
                criteria_list_i = add_UpdateDatabase(Criteria_id, LoadOrder, ExtraStr + "_"+str(i), Folder)
                for xi in criteria_list_i:
                    new_file_lines.append(xi)

            new_file_lines.append(line_i)

        else:
            new_file_lines.append(line_i)
        pass

    file_ins.seek(0)
    for xi in new_file_lines:
        file_ins.write(xi)

    # 关闭文件
    file_ins.close()


def create_config_parameter_file_startbias_terrains(my_terrains, my_features, my_resources):
    sql_filename = "FCAC_Parameters_StartBiasTerrains"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_SBT", "Game",
                    "CONFIG_INFINITE_FREE_SBT", 0,
                    "LOC_PAR_INFINITE_FREE_SBT_DESCRIPTION", "DOMAIN_INFINITE_FREE_SBT", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_SBT_NAME", 4006]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)

    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_dv_info_0 = ["DOMAIN_INFINITE_FREE_SBT", 0,
                    "LOC_DOMAIN_INFINITE_FREE_SBT_0_NAME",
                    "LOC_DOMAIN_INFINITE_FREE_SBT_0_DESCRIPTION",
                    0]
    dv_item_info.append(uu_dv_info_0)

    uu_total_list = []
    uu_name = []

    print("my_terrains.iterrows")
    for index, row in my_terrains.iterrows():
        # print(row["TerrainType"])
        uu_total_list.append(row["TerrainType"])
        uu_name.append(row["Name"])

    for i in range(len(uu_total_list)):
        uu_dv_info = ["DOMAIN_INFINITE_FREE_SBT", i + 1,
                      uu_name[i],
                      uu_name[i],
                      i + 1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(uu_name)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # return

    # 创建对应的选择性加载sql
    uu_generate_file_folder = (
                "C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
                + "\Data\\FreeChooseAbilities\\StartBiasTerrains")
    uu_sql_filename_0 = "FCAC_SBT_0"
    uu_sql_path_0 = generate_sql_file(uu_generate_file_folder, uu_sql_filename_0, author)

    civ_traits_name = "StartBiasTerrains"
    civ_traits_key_list = ["CivilizationType", "TerrainType", "Tier"]

    uu_sql_file_list = []
    uu_sql_file_list.append(uu_sql_path_0)
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]
        trait_kind = unit
        cv_info = ["CIVILIZATION_INFINITE", trait_kind, 1]

        uu_sql_filename_i = "FCAC_SBT_" + str(i + 1)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        insert_into_table_select_where_exists(uu_sql_path_i, civ_traits_name, civ_traits_key_list, cv_info,
                                              extra_order="or ignore ", table1="Terrains", key1="TerrainType",
                                              value1=trait_kind)

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/StartBiasTerrains"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_SBT", "FCAC_SBT")
    pass


def create_config_parameter_file_startbias_terrains_tiers():
    sql_filename = "FCAC_Parameters_StartBiasTerrains_Tiers"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_SBT_T", "Game",
                    "CONFIG_INFINITE_FREE_SBT_T", 1,
                    "LOC_PAR_INFINITE_FREE_SBT_T_DESCRIPTION", "DOMAIN_INFINITE_FREE_SBT_T", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_SBT_T_NAME", 4007]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)

    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_total_list = []
    uu_name = []
    uu_desc = []

    for i in range(5):
        # print(row["TerrainType"])
        uu_total_list.append(i+1)
        uu_name.append("LOC_DOMAIN_INFINITE_FREE_SBT_T_"+str(i+1)+"_NAME")
        uu_desc.append("LOC_DOMAIN_INFINITE_FREE_SBT_T_"+str(i+1)+"_DESCRIPTION")

    for i in range(len(uu_total_list)):
        uu_dv_info = ["DOMAIN_INFINITE_FREE_SBT_T", i+1,
                      uu_name[i],
                      uu_desc[i],
                      i+1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(uu_name)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # return

    # 创建对应的选择性加载sql
    uu_generate_file_folder = (
            "C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
            + "\Data\\FreeChooseAbilities\\StartBiasTerrains_Tiers")

    uu_sql_file_list = []
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]

        sb_table_name = "StartBiasTerrains"

        uu_sql_filename_i = "FCAC_SBT_T_" + str(unit)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        update_table_set_value_where_key_equals(uu_sql_path_i, sb_table_name,
                                                "Tier", unit,
                                                "CivilizationType", "CIVILIZATION_INFINITE")

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/StartBiasTerrains_Tiers"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_SBT_T", "FCAC_SBT_T", loadorder=310000)
    pass


def create_config_parameter_file_startbias_features(my_terrains, my_features, my_resources):
    sql_filename = "FCAC_Parameters_StartBiasFeatures"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_SBF", "Game",
                    "CONFIG_INFINITE_FREE_SBF", 0,
                    "LOC_PAR_INFINITE_FREE_SBF_DESCRIPTION", "DOMAIN_INFINITE_FREE_SBF", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_SBF_NAME", 4008]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)

    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_dv_info_0 = ["DOMAIN_INFINITE_FREE_SBF", 0,
                    "LOC_DOMAIN_INFINITE_FREE_SBF_0_NAME",
                    "LOC_DOMAIN_INFINITE_FREE_SBF_0_DESCRIPTION",
                    0]
    dv_item_info.append(uu_dv_info_0)

    uu_total_list = []
    uu_name = []

    print("my_features.iterrows")
    mustNotHaveStrList = ["BURN"]
    for index, row in my_features.iterrows():
        isValid = True
        for xi in mustNotHaveStrList:
            if xi in row["FeatureType"]:
                isValid = False
        if isValid:
            uu_total_list.append(row["FeatureType"])
            uu_name.append(row["Name"])

    for i in range(len(uu_total_list)):
        uu_dv_info = ["DOMAIN_INFINITE_FREE_SBF", i + 1,
                      uu_name[i],
                      uu_name[i],
                      i + 1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(uu_name)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # return

    # 创建对应的选择性加载sql
    uu_generate_file_folder = (
                "C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
                + "\Data\\FreeChooseAbilities\\StartBiasFeatures")
    uu_sql_filename_0 = "FCAC_SBF_0"
    uu_sql_path_0 = generate_sql_file(uu_generate_file_folder, uu_sql_filename_0, author)

    civ_traits_name = "StartBiasFeatures"
    civ_traits_key_list = ["CivilizationType", "FeatureType", "Tier"]

    uu_sql_file_list = []
    uu_sql_file_list.append(uu_sql_path_0)
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]
        trait_kind = unit
        cv_info = ["CIVILIZATION_INFINITE", trait_kind, 1]

        uu_sql_filename_i = "FCAC_SBF_" + str(i + 1)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        insert_into_table_select_where_exists(uu_sql_path_i, civ_traits_name, civ_traits_key_list, cv_info,
                                              extra_order="or ignore ", table1="Features", key1="FeatureType",
                                              value1=trait_kind)

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/StartBiasFeatures"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_SBF", "FCAC_SBF")
    pass


def create_config_parameter_file_startbias_features_tiers():
    sql_filename = "FCAC_Parameters_StartBiasFeatures_Tiers"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_SBF_T", "Game",
                    "CONFIG_INFINITE_FREE_SBF_T", 1,
                    "LOC_PAR_INFINITE_FREE_SBF_T_DESCRIPTION", "DOMAIN_INFINITE_FREE_SBF_T", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_SBF_T_NAME", 4009]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)

    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_total_list = []
    uu_name = []
    uu_desc = []

    for i in range(5):
        uu_total_list.append(i+1)
        uu_name.append("LOC_DOMAIN_INFINITE_FREE_SBF_T_"+str(i+1)+"_NAME")
        uu_desc.append("LOC_DOMAIN_INFINITE_FREE_SBF_T_"+str(i+1)+"_DESCRIPTION")

    for i in range(len(uu_total_list)):
        uu_dv_info = ["DOMAIN_INFINITE_FREE_SBF_T", i+1,
                      uu_name[i],
                      uu_desc[i],
                      i+1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(uu_name)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # return

    # 创建对应的选择性加载sql
    uu_generate_file_folder = (
            "C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
            + "\Data\\FreeChooseAbilities\\StartBiasFeatures_Tiers")

    uu_sql_file_list = []
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]

        sb_table_name = "StartBiasFeatures"

        uu_sql_filename_i = "FCAC_SBF_T_" + str(unit)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        update_table_set_value_where_key_equals(uu_sql_path_i, sb_table_name,
                                                "Tier", unit,
                                                "CivilizationType", "CIVILIZATION_INFINITE")

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/StartBiasFeatures_Tiers"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_SBF_T", "FCAC_SBF_T", loadorder=310000)
    pass


def create_config_parameter_file_startbias_resources(my_terrains, my_features, my_resources):
    sql_filename = "FCAC_Parameters_StartBiasResources"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_SBR", "Game",
                    "CONFIG_INFINITE_FREE_SBR", 0,
                    "LOC_PAR_INFINITE_FREE_SBR_DESCRIPTION", "DOMAIN_INFINITE_FREE_SBR", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_SBR_NAME", 4010]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)

    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_dv_info_0 = ["DOMAIN_INFINITE_FREE_SBR", 0,
                    "LOC_DOMAIN_INFINITE_FREE_SBR_0_NAME",
                    "LOC_DOMAIN_INFINITE_FREE_SBR_0_DESCRIPTION",
                    0]
    dv_item_info.append(uu_dv_info_0)

    uu_total_list = []
    uu_name = []

    print("my_resources.iterrows")
    for index, row in my_resources.iterrows():
        uu_total_list.append(row["ResourceType"])
        uu_name.append(row["Name"])

    for i in range(len(uu_total_list)):
        uu_dv_info = ["DOMAIN_INFINITE_FREE_SBR", i + 1,
                      uu_name[i],
                      uu_name[i],
                      i + 1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(uu_name)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # return

    # 创建对应的选择性加载sql
    uu_generate_file_folder = (
                "C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
                + "\Data\\FreeChooseAbilities\\StartBiasResources")
    uu_sql_filename_0 = "FCAC_SBR_0"
    uu_sql_path_0 = generate_sql_file(uu_generate_file_folder, uu_sql_filename_0, author)

    civ_traits_name = "StartBiasResources"
    civ_traits_key_list = ["CivilizationType", "ResourceType", "Tier"]

    uu_sql_file_list = []
    uu_sql_file_list.append(uu_sql_path_0)
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]
        trait_kind = unit
        cv_info = ["CIVILIZATION_INFINITE", trait_kind, 1]

        uu_sql_filename_i = "FCAC_SBR_" + str(i + 1)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        insert_into_table_select_where_exists(uu_sql_path_i, civ_traits_name, civ_traits_key_list, cv_info,
                                              extra_order="or ignore ", table1="Resources", key1="ResourceType",
                                              value1=trait_kind)

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/StartBiasResources"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_SBR", "FCAC_SBR")
    pass


def create_config_parameter_file_startbias_resources_tiers():
    sql_filename = "FCAC_Parameters_StartBiasResources_Tiers"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_SBR_T", "Game",
                    "CONFIG_INFINITE_FREE_SBR_T", 1,
                    "LOC_PAR_INFINITE_FREE_SBR_T_DESCRIPTION", "DOMAIN_INFINITE_FREE_SBR_T", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_SBR_T_NAME", 4011]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)

    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_total_list = []
    uu_name = []
    uu_desc = []

    for i in range(5):
        uu_total_list.append(i+1)
        uu_name.append("LOC_DOMAIN_INFINITE_FREE_SBR_T_"+str(i+1)+"_NAME")
        uu_desc.append("LOC_DOMAIN_INFINITE_FREE_SBR_T_"+str(i+1)+"_DESCRIPTION")

    for i in range(len(uu_total_list)):
        uu_dv_info = ["DOMAIN_INFINITE_FREE_SBR_T", i+1,
                      uu_name[i],
                      uu_desc[i],
                      i+1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(uu_name)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # return

    # 创建对应的选择性加载sql
    uu_generate_file_folder = (
            "C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
            + "\Data\\FreeChooseAbilities\\StartBiasResources_Tiers")

    uu_sql_file_list = []
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]

        sb_table_name = "StartBiasResources"

        uu_sql_filename_i = "FCAC_SBR_T_" + str(unit)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        update_table_set_value_where_key_equals(uu_sql_path_i, sb_table_name,
                                                "Tier", unit,
                                                "CivilizationType", "CIVILIZATION_INFINITE")

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/StartBiasResources_Tiers"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_SBR_T", "FCAC_SBR_T", loadorder=310000)
    pass


def create_config_parameter_file_startbias_rivers():
    sql_filename = "FCAC_Parameters_StartBiasRivers"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_SB_R", "Game",
                    "CONFIG_INFINITE_FREE_SB_R", 0,
                    "LOC_PAR_INFINITE_FREE_SB_R_DESCRIPTION", "DOMAIN_INFINITE_FREE_SB_R", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_SB_R_NAME", 4012]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)

    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_dv_info_0 = ["DOMAIN_INFINITE_FREE_SB_R", 0,
                    "LOC_DOMAIN_INFINITE_FREE_SB_R_0_NAME",
                    "LOC_DOMAIN_INFINITE_FREE_SB_R_0_DESCRIPTION",
                    0]
    dv_item_info.append(uu_dv_info_0)

    uu_total_list = []

    for i in range(1):
        uu_total_list.append(i+1)

    for i in range(len(uu_total_list)):
        uu_dv_info = ["DOMAIN_INFINITE_FREE_SB_R", i + 1,
                      "LOC_DOMAIN_INFINITE_FREE_SB_R_{}_NAME".format(i + 1),
                      "LOC_DOMAIN_INFINITE_FREE_SB_R_{}_DESCRIPTION".format(i + 1),
                      i + 1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(uu_total_list)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # return

    # 创建对应的选择性加载sql
    uu_generate_file_folder = (
                "C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
                + "\Data\\FreeChooseAbilities\\StartBiasRivers")
    uu_sql_filename_0 = "FCAC_SB_R_0"
    uu_sql_path_0 = generate_sql_file(uu_generate_file_folder, uu_sql_filename_0, author)

    civ_traits_name = "StartBiasRivers"
    civ_traits_key_list = ["CivilizationType", "Tier"]

    uu_sql_file_list = []
    uu_sql_file_list.append(uu_sql_path_0)
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]
        cv_info = ["CIVILIZATION_INFINITE", 1]

        uu_sql_filename_i = "FCAC_SB_R_" + str(unit)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        insert_into_table_values(uu_sql_path_i, civ_traits_name, civ_traits_key_list, cv_info,
                                 extra_order="or replace ", multi_rows=False)

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/StartBiasRivers"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_SB_R", "FCAC_SB_R")
    pass


def create_config_parameter_file_startbias_rivers_tiers():
    sql_filename = "FCAC_Parameters_StartBiasRivers_Tiers"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_SB_R_T", "Game",
                    "CONFIG_INFINITE_FREE_SB_R_T", 1,
                    "LOC_PAR_INFINITE_FREE_SB_R_T_DESCRIPTION", "DOMAIN_INFINITE_FREE_SB_R_T", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_SB_R_T_NAME", 4013]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)

    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_total_list = []
    uu_name = []
    uu_desc = []

    for i in range(5):
        uu_total_list.append(i+1)
        uu_name.append("LOC_DOMAIN_INFINITE_FREE_SB_R_T_"+str(i+1)+"_NAME")
        uu_desc.append("LOC_DOMAIN_INFINITE_FREE_SB_R_T_"+str(i+1)+"_DESCRIPTION")

    for i in range(len(uu_total_list)):
        uu_dv_info = ["DOMAIN_INFINITE_FREE_SB_R_T", i+1,
                      uu_name[i],
                      uu_desc[i],
                      i+1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(uu_name)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # return

    # 创建对应的选择性加载sql
    uu_generate_file_folder = (
            "C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
            + "\Data\\FreeChooseAbilities\\StartBiasRivers_Tiers")

    uu_sql_file_list = []
    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]

        sb_table_name = "StartBiasRivers"

        uu_sql_filename_i = "FCAC_SB_R_T_" + str(unit)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        update_table_set_value_where_key_equals(uu_sql_path_i, sb_table_name,
                                                "Tier", unit,
                                                "CivilizationType", "CIVILIZATION_INFINITE")

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/StartBiasRivers_Tiers"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_SB_R_T", "FCAC_SB_R_T", loadorder=310000)
    pass


def generate_random_abilities_sql_file():
    sql_filename = "FCAC_Parameters_Random"
    generate_file_folder = "C:\\Users\\User\\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization\\Config"
    author = "Maple_Leaves"
    sql_path = generate_sql_file(generate_file_folder, sql_filename, author)

    para_table_name = "Parameters"
    para_key_list = ["Key1", "Key2", "ParameterId", "ConfigurationGroup",
                     "ConfigurationId", "DefaultValue",
                     "Description", "Domain", "GroupId",
                     "Name", "SortIndex"]
    para_uu_info = ["Ruleset", "RULESET_EXPANSION_2", "PAR_INFINITE_FREE_RANDOM", "Game",
                    "CONFIG_INFINITE_FREE_RANDOM", 0,
                    "LOC_PAR_INFINITE_FREE_RANDOM_DESCRIPTION", "DOMAIN_INFINITE_FREE_RANDOM", "GameOptions",
                    "LOC_PAR_INFINITE_FREE_RANDOM_NAME", 3998]

    insert_into_table_values(sql_path, para_table_name, para_key_list, para_uu_info,
                             extra_order="", multi_rows=False)
    add_empty_rows_to_sql(sql_path, 2)

    # 然后新增可选的值
    domain_value_table_name = "DomainValues"
    domain_value_key_list = ["Domain", "Value", "Name", "Description", "SortIndex"]

    dv_item_info = []

    uu_dv_info_0 = ["DOMAIN_INFINITE_FREE_RANDOM", 0,
                    "LOC_DOMAIN_INFINITE_FREE_RANDOM_0_NAME",
                    "LOC_DOMAIN_INFINITE_FREE_RANDOM_0_DESCRIPTION",
                    0]
    dv_item_info.append(uu_dv_info_0)

    uu_total_list = []

    for i in range(1):
        uu_total_list.append(i + 1)

    for i in range(len(uu_total_list)):
        uu_dv_info = ["DOMAIN_INFINITE_FREE_RANDOM", i + 1,
                      "LOC_DOMAIN_INFINITE_FREE_RANDOM_{}_NAME".format(i + 1),
                      "LOC_DOMAIN_INFINITE_FREE_RANDOM_{}_DESCRIPTION".format(i + 1),
                      i + 1]
        dv_item_info.append(uu_dv_info)

    uu_num = len(uu_total_list)

    insert_into_table_values(sql_path, domain_value_table_name, domain_value_key_list, dv_item_info,
                             extra_order="or replace ", multi_rows=True)
    add_empty_rows_to_sql(sql_path, 2)

    # 创建对应的选择性加载sql
    uu_generate_file_folder = (
            "C:\\Users\\User\Documents\\Firaxis ModBuddy\\Civilization VI\\FreeChooseAbilityCivilization\\FreeChooseAbilityCivilization"
            + "\Data\\FreeChooseAbilities\\Random")
    uu_sql_filename_0 = "FCAC_RANDOM_0"
    uu_sql_path_0 = generate_sql_file(uu_generate_file_folder, uu_sql_filename_0, author)

    uu_sql_file_list = []
    uu_sql_file_list.append(uu_sql_path_0)

    table_names = ["FCAC_UATraits", "FCAC_LATraits", "FCAC_UnitTraits",
                   "FCAC_UITraits", "FCAC_UDTraits", "FCAC_UBTraits"]

    for i in range(len(uu_total_list)):
        unit = uu_total_list[i]

        uu_sql_filename_i = "FCAC_RANDOM_" + str(unit)
        uu_sql_file_list.append(uu_sql_filename_i)
        uu_sql_path_i = generate_sql_file(uu_generate_file_folder, uu_sql_filename_i, author)

        for table_i in table_names:
            key_list = ["TraitType"]
            key_type_list = ["text"]
            key_suffix_list = ["not null"]
            create_table_if_not_exists(uu_sql_path_i, table_i, key_list, key_type_list, key_suffix_list,
                                       primary_key="TraitType")
            add_empty_rows_to_sql(uu_sql_path_i, 2)

        delete_from_where_key_equals(uu_sql_path_i, "CivilizationTraits",
                                     "CivilizationType", "CIVILIZATION_INFINITE")
        add_empty_rows_to_sql(uu_sql_path_i, 2)

        delete_from_where_key_equals(uu_sql_path_i, "LeaderTraits",
                                     "LeaderType", "LEADER_INFINITE")
        add_empty_rows_to_sql(uu_sql_path_i, 2)

    # 创建相应的ActionCriteria
    uu_folder = "Data/FreeChooseAbilities/Random"
    add_to_modinfo_file(uu_total_list, uu_sql_file_list, uu_folder,
                        "CONFIG_INFINITE_FREE_RANDOM", "FCAC_RANDOM", loadorder=320000)
    return uu_generate_file_folder+"\\"+uu_sql_file_list[1]+".sql", table_names


def insert_into_random_sql_file():
    """
    insert or ignore into {}
    key1, key2
    select
    'A', b1
    from {}
    where b1 in (select b2 from {})
    order by RANDOM() limit 1 ;
    """

    insert_into_global_parameter_fcac_tables_count(random_sql_path, fcac_table_names, extra_order="or replace ")
    add_empty_rows_to_sql(random_sql_path, 2)

    for table_i in fcac_table_names:
        if "UnitTraits" in table_i:
            insert_into_select_limit_offset(random_sql_path, "CivilizationTraits",
                                            "CivilizationType", "TraitType",
                                            "CIVILIZATION_INFINITE", "TraitType",
                                            "Traits", table_i, "TraitType",
                                            extra_order="or ignore ",
                                            seed_1=197, seed_2=211, seed_3=199)
            add_empty_rows_to_sql(random_sql_path, 2)

            insert_into_select_limit_offset(random_sql_path, "LeaderTraits",
                                            "LeaderType", "TraitType",
                                            "LEADER_INFINITE", "TraitType",
                                            "Traits", table_i, "TraitType",
                                            extra_order="or ignore ",
                                            seed_1=157, seed_2=167, seed_3=163)
            add_empty_rows_to_sql(random_sql_path, 2)

        elif "UATraits" in table_i:
            insert_into_select_limit_offset(random_sql_path, "CivilizationTraits",
                                            "CivilizationType", "TraitType",
                                            "CIVILIZATION_INFINITE", "TraitType",
                                            "Traits", table_i, "TraitType",
                                            extra_order="or ignore ",
                                            seed_1=167, seed_2=179, seed_3=173)
            add_empty_rows_to_sql(random_sql_path, 2)

        elif "LATraits" in table_i:
            insert_into_select_limit_offset(random_sql_path, "CivilizationTraits",
                                            "CivilizationType", "TraitType",
                                            "CIVILIZATION_INFINITE", "TraitType",
                                            "Traits", table_i, "TraitType",
                                            extra_order="or ignore ",
                                            seed_1=257, seed_2=269, seed_3=263)
            add_empty_rows_to_sql(random_sql_path, 2)

        elif "UITraits" in table_i:
            insert_into_select_limit_offset(random_sql_path, "CivilizationTraits",
                                            "CivilizationType", "TraitType",
                                            "CIVILIZATION_INFINITE", "TraitType",
                                            "Traits", table_i, "TraitType",
                                            extra_order="or ignore ",
                                            seed_1=83, seed_2=97, seed_3=89)
            add_empty_rows_to_sql(random_sql_path, 2)

        elif "UBTraits" in table_i:
            insert_into_select_limit_offset(random_sql_path, "CivilizationTraits",
                                            "CivilizationType", "TraitType",
                                            "CIVILIZATION_INFINITE", "TraitType",
                                            "Traits", table_i, "TraitType",
                                            extra_order="or ignore ",
                                            seed_1=47, seed_2=59, seed_3=53)
            add_empty_rows_to_sql(random_sql_path, 2)

        elif "UDTraits" in table_i:
            insert_into_select_limit_offset(random_sql_path, "CivilizationTraits",
                                            "CivilizationType", "TraitType",
                                            "CIVILIZATION_INFINITE", "TraitType",
                                            "Traits", table_i, "TraitType",
                                            extra_order="or ignore ",
                                            seed_1=43, seed_2=53, seed_3=47)
            add_empty_rows_to_sql(random_sql_path, 2)
    pass


def simulate_random_number(m, n, p, item_num):
    random_number_list = []
    for i in range(5):
        year = 2024+i
        for j in range(12):
            month = j+1
            for k in range(31):
                day = k+1

                # random_number = (year*m+j*n+day*p) % item_num
                random_number = (year * m + j * n + day * p) % item_num
                random_number_list.append(random_number)
                pass

    pass

    n, bins, patches = plt.hist(random_number_list, item_num)
    plt.show()


if __name__ == '__main__':
    my_civ_traits, my_leader_traits = read_sqlite()

    print("================================================")
    players, playerItems = read_configuration()

    # item_has_trait("UNIT_HETAIROI", my_civ_traits, my_leader_traits)

    # unique_units = get_unique_units(my_civ_traits, my_leader_traits, players, playerItems)

    add_localized_text()
    time.sleep(0.1)

    # addToRandomFile = False

    random_sql_path, fcac_table_names = generate_random_abilities_sql_file()
    print("random_sql_path=\t", random_sql_path)
    time.sleep(0.1)

    create_config_parameter_file_uu(my_civ_traits, my_leader_traits, players, playerItems)
    time.sleep(0.1)
    create_config_parameter_file_lu(my_civ_traits, my_leader_traits, players, playerItems)
    time.sleep(0.1)
    create_config_parameter_file_ui(my_civ_traits, my_leader_traits, players, playerItems)
    time.sleep(0.1)
    create_config_parameter_file_ud(my_civ_traits, my_leader_traits, players, playerItems)
    time.sleep(0.1)
    create_config_parameter_file_ub(my_civ_traits, my_leader_traits, players, playerItems)
    time.sleep(0.1)
    create_config_parameter_file_ua(my_civ_traits, my_leader_traits, players, playerItems)
    time.sleep(0.1)
    create_config_parameter_file_la(my_civ_traits, my_leader_traits, players, playerItems)
    time.sleep(0.1)

    insert_into_random_sql_file()

    my_terrains, my_features, my_resources = read_sqlite_startbias()
    create_config_parameter_file_startbias_terrains(my_terrains, my_features, my_resources)
    time.sleep(0.1)
    create_config_parameter_file_startbias_terrains_tiers()
    time.sleep(0.1)

    create_config_parameter_file_startbias_features(my_terrains, my_features, my_resources)
    time.sleep(0.1)
    create_config_parameter_file_startbias_features_tiers()
    time.sleep(0.1)

    create_config_parameter_file_startbias_resources(my_terrains, my_features, my_resources)
    time.sleep(0.1)
    create_config_parameter_file_startbias_resources_tiers()
    time.sleep(0.1)

    create_config_parameter_file_startbias_rivers()
    time.sleep(0.1)
    create_config_parameter_file_startbias_rivers_tiers()
    time.sleep(0.1)

    delete_criterias(16)

    # simulate_random_number(167, 179, 173, 50)
    # simulate_random_number(257, 269, 263, 75)
    # simulate_random_number(197, 211, 199, 58)
    # simulate_random_number(157, 167, 163, 58)
    # simulate_random_number(83, 97, 89, 23)
    # simulate_random_number(43, 53, 47, 15)
    # simulate_random_number(47, 59, 53, 16)
    pass
